package cn.kgc.controller;    /*
 *@author wangchangsheng
 *@Date 2023/4/27
 */

import cn.kgc.bean.pojo.EpDepartment;

import cn.kgc.bean.pojo.EpStaff;
import cn.kgc.bean.vo.Result;
import cn.kgc.service.EpDepartmentService;
import cn.kgc.service.EpStaffService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/epDepartment")
public class EpDepartmentController {

    @Resource
    private EpDepartmentService epDepartmentService;



    @PostMapping("/addEpDepartment")
    public Result addEpDepartment(@RequestBody EpDepartment epDepartment){
        EpDepartment epDepartment1 = epDepartmentService.getIdEpDepartment();
        Long id = epDepartment1.getId()+1;
        epDepartment.setDepId("DE"+id);
        boolean flag=epDepartmentService.addEpDepartment(epDepartment);

        if (flag){
            return Result.success(flag);
        }else {
            return Result.failure();
        }

    }


    @GetMapping("/getEpDepartmentDepName")
    public Result getEpDepartmentDepName(){
        List<String> list = epDepartmentService.getEpDepartmentDepName();
        return Result.success(list);
    }
    @GetMapping("/getAllInfoEpDepartment/{pageFrom}/{pageSize}")
    public Result getAllInfoEpDepartment(@PathVariable Integer pageFrom,
                                         @PathVariable Integer pageSize){

        PageHelper.startPage(pageFrom,pageSize);
        List<EpDepartment> list = epDepartmentService.getAllInfoEpDepartment();
        for (int i = 0;i<list.size();i++){
            if (list.get(i).getTotalVio()==null){
                list.get(i).setTotalVio(0);
            }
            if (list.get(i).getSumDangerous()==null){
                list.get(i).setSumDangerous(0);
            }
        }
        PageInfo pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @RequestMapping("/updateEpDepartment")
    public Result updateEpDepartment(@RequestBody EpDepartment epDepartment){
        boolean flag=epDepartmentService.updateEpDepartment(epDepartment);
        if (flag){
            return Result.success(flag);
        }else {
            return Result.failure();
        }
    }

    @RequestMapping("/delEpDepartment")
    public Result delEpDepartment(@RequestParam Long id){
        boolean flag=epDepartmentService.delEpDepartment(id);
        if (flag){
            return Result.success(flag);
        }else {
            return Result.failure();
        }
    }
}
